﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKArchUtils.Utils;
using YJKRevitKernel.RevitTopologySuite;
using YJKRevitDimensionText.OneKeyPlanDim.LevelDim.Data;

namespace YJKRevitDimensionText.OneKeyPlanDim.LevelDim
{
	public class FloorLevelDim
	{
		public FloorLevelDim(Autodesk.Revit.DB.Document doc)
		{
			this.m_doc = doc;
		}

		public void AddFloor(Floor floor)
		{
			FloorGeometry floorGeometry = new FloorGeometry(floor);
			if (floorGeometry.m_planarFace != null)
			{
				this.m_lstFloorGeometry.Add(floorGeometry);
			}
		}

		public List<FloorGeometry> GetFloor()
		{
			return this.m_lstFloorGeometry;
		}

		public void CreateLevelDim()
		{
			this.FilterCoverFloor();
			this.FloorClassification();
			this.m_lstCommonLayerFloor.Sort(new CommonLayerFloorComparer());
			foreach (CommonLayerFloor commonLayerFloor in this.m_lstCommonLayerFloor)
			{
				commonLayerFloor.CreateLevelDim();
			}
		}

		private void FloorClassification()
		{
			Level genLevel = this.m_doc.ActiveView.GenLevel;
			if (genLevel == null)
			{
				return;
			}
			string strB = (genLevel.Elevation / 1000.0).ToString("f3");
			using (List<FloorGeometry>.Enumerator enumerator = this.m_lstFloorGeometry.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FloorGeometry floorGeometry = enumerator.Current;
					CommonLayerFloor commonLayerFloor = this.m_lstCommonLayerFloor.Find((CommonLayerFloor n) => n.LevelVal.Equals(floorGeometry.m_strLevelVal));
					if (commonLayerFloor == null)
					{
						commonLayerFloor = new CommonLayerFloor(this.m_doc);
						commonLayerFloor.AddFloor(floorGeometry);
						commonLayerFloor.dLevelVal = floorGeometry.m_dLevelVal;
						commonLayerFloor.IsCurrentLay = false;
						if (floorGeometry.m_strLevelVal.CompareTo(strB) == 0)
						{
							commonLayerFloor.IsCurrentLay = true;
						}
						this.m_lstCommonLayerFloor.Add(commonLayerFloor);
					}
					else
					{
						commonLayerFloor.AddFloor(floorGeometry);
					}
				}
			}
		}

		private void FilterCoverFloor()
		{
			List<FloorGeometry> list = new List<FloorGeometry>();
			foreach (FloorGeometry floorGeometry in this.m_lstFloorGeometry)
			{
				if (floorGeometry.m_lstBaseCv.Count != 0)
				{
					RevitPolygonDataBean revitPolygonDataBean = null;
					try
					{
						revitPolygonDataBean = new RevitPolygonDataBean(floorGeometry.m_lstBaseCv, null, 3, 4u);
					}
					catch
					{
						continue;
					}
					foreach (FloorGeometry floorGeometry2 in this.m_lstFloorGeometry)
					{
						if (!(floorGeometry.m_Floor.Id == floorGeometry2.m_Floor.Id) && floorGeometry2.m_lstBaseCv.Count != 0)
						{
							RevitPolygonDataBean revitPolygonDataBean2 = null;
							try
							{
								revitPolygonDataBean2 = new RevitPolygonDataBean(floorGeometry2.m_lstBaseCv, null, 3, 4u);
							}
							catch
							{
								continue;
							}
							RevitPolygonBooleanOperater revitPolygonBooleanOperater = new RevitPolygonBooleanOperater();
							List<RevitOperationDataBean> list2 = null;
                            if (revitPolygonBooleanOperater.TryGetBooleanResult(revitPolygonDataBean, revitPolygonDataBean2, 0, out list2))
							{
								double num = 0.0;
								foreach (RevitOperationDataBean revitOperationDataBean in list2)
								{
									RevitPolygonDataBean revitPolygonDataBean3 = revitOperationDataBean as RevitPolygonDataBean;
									if (revitPolygonDataBean3 != null && revitPolygonDataBean3.Area != null)
									{
										num += revitPolygonDataBean3.Area.Value;
									}
								}
								FloorGeometry floorGeometry3 = floorGeometry;
								if (Geometry.LessThan(floorGeometry2.m_dLevelVal, floorGeometry.m_dLevelVal))
								{
									floorGeometry3 = floorGeometry2;
								}
								if (Geometry.LessThan(floorGeometry3.m_planarFace.Area / 4.0, num))
								{
									list.Add(floorGeometry3);
								}
							}
						}
					}
				}
			}
			foreach (FloorGeometry floorGeometry4 in list)
			{
				Floor floor = floorGeometry4.m_Floor;
				if (floor != null && !floor.Name.Contains("室外"))
				{
					this.m_lstFloorGeometry.Remove(floorGeometry4);
				}
			}
		}

		private Document m_doc;

		private List<CommonLayerFloor> m_lstCommonLayerFloor = new List<CommonLayerFloor>();

		private List<FloorGeometry> m_lstFloorGeometry = new List<FloorGeometry>();
	}
}
